package com.example.netty;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

/**
 * TODO: 这里要写注释的!
 */
public class EchoClientHandler extends ChannelHandlerAdapter {
    private int counter=0;
    private static final String ECHO_REQ= "hi, bruce. welcome to Netty.$_";

    @Override
    public void channelActive(ChannelHandlerContext context){
        for (int i = 0; i < 100; i++) {
            context.writeAndFlush(Unpooled.copiedBuffer(ECHO_REQ.getBytes()));
        }
    }

    @Override
    public void channelRead(ChannelHandlerContext context,Object msg){
        System.out.println("this is " + ++counter + " times receive client : [ " + msg + " ]");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext var1, Throwable var2) throws Exception{
        var2.printStackTrace();
        var1.close();
    }
}
